﻿using EMS.system.Entityies.MasterData.WorkstationModel;
using EMS.system.Entityies.ProductionManagement.ProductionWorkOrderModel;
using EMS.system.Entityies.WarehouseManagement.ProductionRequisitionModel;
using EMS.system.IEntityAppService.WarehouseManagement.MaterialRequisition;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using System;
using EMS.system.Common;
using EMS.system.IEntityAppService.WarehouseManagement.WarehouseSetup;
using EMS.system.EntityAppService.WarehouseManagement.WarehouseSetup;
using Humanizer;

namespace EMS.system.Web.Controllers.WarehouseManagement.MaterialRequisition
{
    public class ProductionRequisitionController : Controller
    {
        private readonly IProductionRequisitionAppService _service;

        public ProductionRequisitionController(IProductionRequisitionAppService service)
        {
            _service = service;
        }

        public IActionResult Add()
        {
            return View();
        }

        public IActionResult Edit()
        {
            return View();
        }

        public IActionResult ProductionRequisitionList()
        {
            return View();
        }

        [HttpPost]
        public async Task<int> AddProductionRequisition(SalesReturnsDTO dto)
        {
            var result = await _service.AddProductionRequisition(dto);
            return result;
        }

        public JsonResult GetProductionRequisition(string? code, string? name, int page = 1, int limit = 2)
        {
            var list = _service.GetProductionRequisition(code, name, page, limit);
            return Json(new { data = list.Queryable, count = list.RowCount, code = "0", msg = "" });
        }

        [HttpPost]
        public Task<int> DeleteProductionRequisition(int id)
        {
            var result =  _service.DeleteProductionRequisition(id);
            return result;

        }

        public JsonResult GetProductionRequisitionById(int id)
        {
            var result = _service.GetProductionRequisitionById(id);
            return Json(new { data = result, code = 0, msg = "" });
        }

        [HttpPost]
        public async Task<int> UpdateProductionRequisition(SalesReturnsDTO dto)
        {
            var list = await _service.UpdateProductionRequisition(dto);
            return list;
        }

        public List<ProductionWorkOrder> GetProductionWorkOrder()
        {
            var list = _service.GetProductionWorkOrder();
            return list;
        }

        public List<Workstation> GetWorkstation()
        {
            var list = _service.GetWorkstation();
            return list;
        }

        [HttpPost]
        public async Task<int> DeleAll(string id)
        {
            int res = 0;
            foreach (var ids in id.Split(","))
            {
                res = await _service.DeleteProductionRequisition(int.Parse(ids));
            }
            return res;
        }
    }
}
